package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Joey
 * @Description:
 * @date:2024/4/19 16:42
 */
@Service
public class SetMealServiceImp implements SetMealService {


    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        if(setmeal!=null){
            setmealMapper.insertSetMeal(setmeal);
        }
        Long setMealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes !=null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setMealId);
            });
            setmealDishMapper.insertBatchSetMeals(setmealDishes);
        }
    }

    @Override
    public PageResult pageSetMeals(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageSetMealQuery(dto);
        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void modifyStatus(Integer status, long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.setStatus(setmeal);
    }

    @Override
    public SetmealVO selectDishById(Integer id) {
        Setmeal setmeal = setmealMapper.queryById(id);
        List<SetmealDish> dishes = setmealDishMapper.queryById(setmeal.getId());
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void modifySetMealInfo(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.updateInfo(setmeal);

        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();

        setmealDishMapper.deleteSetMealDish(setmeal.getId());

        if(dishes !=null && dishes.size()>0){
            for (SetmealDish df:dishes) {
                df.setSetmealId(setmeal.getId()); // null?
                df.setDishId(df.getDishId());
            }
            //修改
            setmealDishMapper.insertBatchSetMeals(dishes);
        }

    }

    @Override
    @Transactional
    public void deleteSetMeal(List<Long> ids) {
        //判断售卖状态 启售(1) 不可删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.queryById(id);
            if(setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //批量、删除套餐数据
        setmealMapper.deleteBatch(ids);
        //批量、删除套餐对应菜品
        setmealDishMapper.deleteDish(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
